package com.lianboke.link.ad.VolleyUtils.uploadImg;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import java.io.ByteArrayOutputStream;

/**
 * Created by moon.zhong on 2015/3/3.
 */
public class FormImage {
    //参数的名称
    private String mName ;
    //文件名
    private String mFileName ;
    //文件的 mime，需要根据文档查询
    private String mMime ;
    //需要上传的图片资源，因为这里测试为了方便起见，
    // 直接把 bigmap 传进来，真正在项目中一般不会这般做，
    // 而是把图片的路径传过来，在这里对图片进行二进制转换
   // private Bitmap mBitmap ;
    private String mImgPath;

    public FormImage(String  imgPath) {
        this.mImgPath = imgPath;
    }

    //获取参数名
    public String getmName() {
        if(TextUtils.isEmpty(mName)){
            return "uploadimg" ;
        }else{
            return mName ;
        }
    }

    public void setmName(String mName) {
        this.mName = mName;
    }

    //获取文件名
    public String getmFileName() {
        if(TextUtils.isEmpty(mFileName)){
           return getFileNameWithSuffix(mImgPath);
        }else{
            return mFileName;
        }
    }

    public void setmFileName(String mFileName) {
        this.mFileName = mFileName;
    }

    //获取类型
    public String getmMime() {
        if(TextUtils.isEmpty(mMime)){
            mMime = getFileSuffix(mImgPath);
        }

        if("png".equalsIgnoreCase(mMime)){
            return "image/png";
        }else if("jpeg".equalsIgnoreCase(mMime) || "jpg".equalsIgnoreCase(mMime)){
            return  " image/jpeg";
        }else{
            return "image/png";
        }
    }

    //设置类型
    public void setmMime(String mMime) {
        this.mMime = mMime;
    }

    //对图片进行二进制转换
    public byte[] getValue() {
        byte[] result = null;
        Bitmap bitmap =null;
        try {
            bitmap = BitmapFactory.decodeFile(mImgPath);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            if ("png".equalsIgnoreCase(mMime)) {
                bitmap.compress(Bitmap.CompressFormat.PNG, 80, bos);
            }else{
                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            }
            result= bos.toByteArray();
        }catch (Exception e){

        }finally {
            if(bitmap!=null){
                bitmap.recycle();
            }
        }
        return result;
    }

    private String getFileName(String pathandname) {
        /**
         * 仅保留文件名不保留后缀
         */
        int start = pathandname.lastIndexOf("/");
        int end = pathandname.lastIndexOf(".");
        if (start != -1 && end != -1) {
            return pathandname.substring(start + 1, end);
        } else {
            return pathandname;
        }
    }
    /**
     * 保留文件名及后缀
     */
    private String getFileNameWithSuffix(String pathandname) {
        int start = pathandname.lastIndexOf("/");
        if (start != -1 ) {
            return pathandname.substring(start + 1);
        } else {
            return pathandname;
        }
    }
    /**
     * 获取文件的后缀
     */
    private String getFileSuffix(String pathandname) {
        int start = pathandname.lastIndexOf(".");
        if (start != -1 ) {
            return pathandname.substring(start + 1);
        } else {
            return pathandname;
        }
    }
}